<!--可以拖动的列-->
<!--有其他的需要加在列上的属性自行添加-->
<!--由于一些问题，需要添加一个宽度为1的列，参考 https://github.com/ElemeFE/element/issues/2808-->
<!--使用范例参考 src/view/queryCensusP/liquidity/index.vue-->
<template>
  <div>
    <el-table-column
      v-for="item in inTableHeader"
      :key="Math.random()"
      :width="item.width"
      :prop="item.prop"
      :label="item.label"
      :align="item.align || 'center'"
      :type="item.type"
      show-overflow-tooltip
    >
      <template v-if="item.component && !(item.children && item.children.length)" v-slot="scope">
        <component :is="item.component" :row="scope" :data="data"></component>
      </template>
      <template v-if="item.children && item.children.length">
        <el-table-column
          v-for="childItem in item.children"
          :key="Math.random()"
          :width="childItem.width"
          :prop="childItem.prop"
          :label="childItem.label"
          :align="childItem.align || 'center'"
          :type="item.type"
          show-overflow-tooltip
        >
          <template v-if="childItem.component && !(childItem.children && childItem.children.length)" v-slot="childScope">
            <component :is="item.component" :row="childScope" :data="data" :self="self"></component>
          </template>
        </el-table-column>
      </template>
    </el-table-column>
  </div>
</template>

<script>
import Sortable from 'sortablejs'

export default {
  name: 'dragTableHeader',
  data() {
    const recursion = items => {
      for (let i = 0; i < items.length; i++) {
        const item = items[i]
        item.key = i
        if (item.children && item.children.length) {
          recursion(item.children)
        }
      }
    }
    const headerItemFilter = item => {
      // v-if 优先
      if (item.if === undefined || item.if === null || item.if === '') {
        if (item.show === undefined || item.show === null || item.show === '') {
          return true
        } else {
          return item.show
        }
      }
      return item.if
    }
    return {
      recursion,
      headerItemFilter,
    }
  },
  props: {
    // 当前页面的数据，可以使用 $data 传入，方便在自定义插槽的的时候使用
    data: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // 表头数据，目前最多支持两层，自定义插槽需要以组件的形式挂载
    tableHeader: {
      type: Array,
      require: true,
      default: () => {
        return []
      },
    },
  },
  computed: {
    inTableHeader() {
      return [
        {
          width: 1,
        },
      ].concat(this.tableHeader.filter(item => this.headerItemFilter(item)))
    },
    childTableHeader() {
      return items => {
        items.filter(item => this.headerItemFilter(item))
      }
    },
  },
  watch: {
    tableHeader: {
      handler: function (val) {
        this.inTableHeader = [
          {
            width: 1,
          },
        ].concat(val.filter(item => this.headerItemFilter(item)))
      },
      deep: true,
    },
  },
  mounted() {
    this.columnDrop()
  },
  methods: {
    //列拖拽
    columnDrop() {
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      const _this = this
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onChoose: function (evt) {},
        onUnchoose: function (evt) {},
        onStart: function (evt) {},
        onEnd: evt => {
          if (!_this.$store.state.app.isLoading) {
            _this.$store.dispatch('app/loading_status', true)
          }
          let oldIndex = evt.oldIndex
          let newIndex = evt.newIndex
          const item = _this.tableHeader[oldIndex]
          // 把元素切换到新的位置
          _this.tableHeader.splice(oldIndex, 1)
          _this.tableHeader.splice(newIndex, 0, item)
          if (_this.$store.state.app.isLoading) {
            _this.$store.dispatch('app/loading_status', false)
          }
        },
      })
    },
  },
}
</script>

<style scoped></style>
